package com.mindskip.xzs.controller.student;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.context.WebContext;
import com.mindskip.xzs.domain.dto.student.exampaper.ExamPaperAnswerDTO;
import com.mindskip.xzs.domain.dto.student.exampaper.ExamPaperAnswerPageStudentDTO;
import com.mindskip.xzs.domain.vo.student.exampaper.ExamPaperAnswerPageStudentVO;
import com.mindskip.xzs.domain.vo.student.exampaper.ExamPaperReadVO;
import com.mindskip.xzs.domain.dto.student.exampaper.ExamPaperSubmitDTO;
import com.mindskip.xzs.service.ExamPaperAnswerService;
import com.mindskip.xzs.utility.RestResponse;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController("StudentExamPaperAnswerController")
@RequestMapping(value = "/api/student/exampaper/answer")
public class ExamPaperAnswerController {

    private final ExamPaperAnswerService examPaperAnswerService;

    private final WebContext webContext;

    public ExamPaperAnswerController(ExamPaperAnswerService examPaperAnswerService, WebContext webContext) {
        this.examPaperAnswerService = examPaperAnswerService;
        this.webContext = webContext;
    }

    // 答卷提交
    @PostMapping("/answerSubmit")
    public RestResponse<String> answerSubmit(@RequestBody @Valid ExamPaperSubmitDTO examPaperSubmitDTO) {

        String score = examPaperAnswerService.answerSubmit(examPaperSubmitDTO, webContext.getCurrentUser());
        return RestResponse.ok(score);
    }

    // 答卷查询
    @PostMapping("/read/{id}")
    public RestResponse<ExamPaperReadVO> read(@PathVariable @Valid Integer id) {

        ExamPaperReadVO examPaperReadVO = examPaperAnswerService.read(id);
        return RestResponse.ok(examPaperReadVO);
    }

    // 试卷批改
    @PostMapping("/edit")
    public RestResponse<String> edit(@RequestBody @Valid ExamPaperAnswerDTO examPaperAnswerDTO) {

        String score = examPaperAnswerService.edit(examPaperAnswerDTO, webContext.getCurrentUser());
        return RestResponse.ok(score);
    }

    // 考试记录分页
    @PostMapping("/pageList")
    public RestResponse<PageInfo<ExamPaperAnswerPageStudentVO>> pageList(@RequestBody @Valid ExamPaperAnswerPageStudentDTO examPaperAnswerPageStudentDTO) {

        examPaperAnswerPageStudentDTO.setCreateUser(webContext.getCurrentUser().getId());
        PageInfo<ExamPaperAnswerPageStudentVO> page = examPaperAnswerService.page(examPaperAnswerPageStudentDTO, webContext.getCurrentUser());
        return RestResponse.ok(page);
    }
}
